package cs2340.todolist.JUnit;

import java.util.Date;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import cs2340.todolist.model.Location;
import cs2340.todolist.model.ToDoItem;
import cs2340.todolist.model.sqlite_database.ToDoItemDataSource;
import cs2340.todolist.model.sqlite_database.UserAccountDataSource;
import cs2340.todolist.splash.Splash;

/**
 * this test case tests the method deleteItem() of ToDoItemDataSource
 * precondition: id has to be greater than -1
 * postcondition: if the item is deleted from the database, return true, if not return false
 * 
 * @author BaoTam
 *
 */
public class TestDeleteItem extends ActivityInstrumentationTestCase2<Splash> {
	ToDoItemDataSource itemDataSource;
	UserAccountDataSource userDataSource;
	Activity activity;
	public TestDeleteItem() {
		super("cs2340.todolist.splash", Splash.class);
	}
	
	public void setUp() throws Exception {
		super.setUp();
		activity = getActivity();
		itemDataSource = new ToDoItemDataSource(activity);
		userDataSource = new UserAccountDataSource(activity);

	}
	
	/**
	 * The test covers invalid partition (id < -1), valid partition (id > -1), and boundary (id = -1)
	 * Within the valid partition, there are two possible situations: id belongs to a non-existent item, 
	 * 		and id belongs to an item already in the database, so the test has covered both of these possibilities.
	 * It also checks if deleting an item guarantees that the item has been completely removed from the database.
	 * 
	 */
	public void testDeleteItem() {
		
		itemDataSource.open();
		userDataSource.open();
		//delete non-existent item (id > -1)
		boolean res = itemDataSource.deleteItem(100);
		//delete with id = -1
		boolean res1 = itemDataSource.deleteItem(-1);
		//delete with id < -1
		boolean res2 = itemDataSource.deleteItem(-10);
		
		long userId = userDataSource.insertUser("testGuy", "", "testUserName", "testPassword");
		long id = itemDataSource.insertItem(userId, "testTitle111", new Location("testLocation0", "Atl"), 0, new Date(), 0, "", false);
		//delete existing item
		boolean res3 = itemDataSource.deleteItem(id);
		
		//check if item still in database;
		ToDoItem item = itemDataSource.getItemById(id);
		userDataSource.deleteUser(userId);
		itemDataSource.close();
		userDataSource.close();
		
		
		assertFalse("delete non-existent item",res);
		assertFalse("delete with id = -1", res1);
		assertFalse("delete with id < -1", res2);
		assertTrue("delete existing item",res3);
		assertEquals("check if item still in database after deletion claimed to be successful", null, item);
	}
	
}
